/* SQUIB - Scala's Quirky User Interface Builder 
    Copyright (C) 2008 Tim Dalton

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*/
    
/**
 *    Factory object to create generic component that delegates calls to paint 
 */    
    
package tfd.scala.squib;

import java.awt.{Dimension, Graphics}
import javax.swing.{JComponent}

object canvas extends IdableTrait with HasSetterAttributes {
    /**
     * Factory method to construct a JComponent with paint delegate. 
     * @param width Preferred width for component
     * @param height Preferred height for component 
     * @param delegate Function object taking a Graphics parameter that perform paint for component
     * Example:
     * <pre>
     * canvas(30,30,(g: Graphics) => {
     *                          		g.setColor(Color.magenta)
     *                           		g.drawLine(0,0,30,30)
     *                           		g.drawLine(30,0,0,30)
     *                               }
     * )  
     * </pre>
     * The above example will draw a magenta "X" 30x30 pixels in size
     */
    
    def apply(width: Int, height: Int, delegate: Graphics => Unit):JComponent = {
         var component = new JComponent {
                override def getPreferredSize() = new Dimension(width,height)
                override def paint(g: Graphics):Unit = delegate(g)
          }
         component
    }
     
     /**
      * Factory method to construct a JComponent with paint delegate. 
      * @param idee Id of component
      * @param width Preferred width for component
      * @param height Preferred height for component 
      * @param delegate Function object taking a Graphics parameter that perform paint for component
      * Example:
      * <pre>
      * canvas("cvs", 30,30,(g: Graphics) => {
      *                                   		g.setColor(Color.red)
      *                                   		g.fillRect(5,5,20,20)
      *                               		 }
      * )  
      * </pre>
      * The above example will draw a red box 20x20 pixels in size
      */
     
    def apply(idee:String, width: Int, height: Int, delegate: Graphics => Unit):JComponent = {
    	var component = apply(width, height, delegate)
        doId(idee,component)
    }
}